package com.jerry.chinesechess.util;

import com.jerry.chinesechess.ChinesechessApplication;
import com.jerry.chinesechess.enums.Player;
import com.jerry.chinesechess.model.Piece;
import com.jerry.chinesechess.model.Position;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import javafx.scene.transform.Rotate;

import static com.jerry.chinesechess.ChinesechessApplication.*;

public class CanvasUtils {

    //移动棋子到指定位置
    public static void moveChess(int i, int j, GraphicsContext gc) {
        if (selectChess == null) {
            return;
        }
        Piece piece = board.getPieceAt(new Position(j, i));
        if (piece != null && piece.getPlayer().equals(selectChess.getPlayer())) {
            selectChess = piece;
            selectChess(selectChess, gc);
            return;
        }

        board.movePiece(selectChess.getPosition(), new Position(j, i));
        gc.clearRect(0, 0, canvasW, canvasH);
        drawShapes(gc);
        board.checkmate();
    }

    //圈棋盘背景
    public static Canvas placeCanvas() {
        Canvas canvas = new Canvas(canvasW * 1.1, canvasH * 1.1);
        GraphicsContext gc = canvas.getGraphicsContext2D();
        Image img = new Image(ChinesechessApplication.class.getResourceAsStream("/image/a.png"));
        gc.drawImage(img, 0, 0, canvasW * 1.09, canvasH * 1.09);
        gc.setFill(Color.valueOf("7A6B37"));
        gc.fillRoundRect(canvasW * 1.09, 0, canvasW * 1.1, canvasH * 1.1, canvasW * 0.01, canvasH * 0.01);
        gc.fillRoundRect(0, canvasH * 1.09, canvasH * 1.1, canvasH * 1.1, canvasW * 0.01, canvasH * 0.01);
        return canvas;
    }

    //写一个翻转的汉界
    public static Canvas rotateCanvas() {
        Canvas canvas2 = new Canvas(canvasW * 1.0, canvasH * 1.0);
        GraphicsContext gc2 = canvas2.getGraphicsContext2D();
        gc2.setFill(Color.GREEN);
        gc2.setStroke(Color.BLUE);
        gc2.setLineWidth(1);
        gc2.setTextAlign(TextAlignment.CENTER);
        gc2.setFont(new Font(fontName, fontSize));
        Rotate r = new Rotate(180, girdW / 2 + girdW * 6, canvasH - girdH * 5 - fontSize / 3);
        gc2.setTransform(r.getMxx(), r.getMyx(), r.getMxy(), r.getMyy(), r.getTx(), r.getTy());
        gc2.fillText("汉界", girdW / 2 + girdW * 6, canvasH - girdH * 5 - fontSize / 3 + 5);
        gc2.restore();
        return canvas2;
    }

    //画棋子
    public static void drawChess(GraphicsContext gc) {
        if (init) {
            defaultPlace(gc);
        } else {
            for (int i = 0; i < 9; i++) {
                System.out.println("\n");
                for (int j = 0; j < 10; j++) {
                    Piece chess = board.getPieceAt(new Position(j, i));
                    if (chess != null) {
                        System.out.print(chess.getType());
                        drawOneChess(gc, chess);
                    } else {
                        System.out.print("  ");
                    }
                }
            }
            if (selectChess != null) {
                gc.setLineWidth(2);
                gc.setStroke(Color.valueOf("EAEED9"));
                selectChess.setSelected(true);
                gc.strokeOval(selectChess.getX(), selectChess.getY(), selectChess.getW(), selectChess.getW());
                selectChess = null;
            }
        }
    }

    //默认棋盘
    public static void defaultPlace(GraphicsContext gc) {
        init = false;
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 10; j++) {
                Piece piece = board.getPieceAt(new Position(j, i));
                if (piece != null) {
                    gc.setFill(Color.WHEAT);
                    drawOneChess(gc, piece);
                }
            }
        }

    }

    public static void drawOneChess(GraphicsContext gc, Piece chess) {
        Color color = Color.RED;
        if (Player.BLACK.equals(chess.getPlayer())) {
            color = Color.BLACK;
        }
        gc.setFont(new Font(fontName, fontSize));
        gc.setFill(Color.WHEAT);
        gc.fillOval(chess.getX(), chess.getY(), chess.getW(), chess.getW());
        gc.setLineWidth(2);
        gc.setStroke(Color.rgb(1, 1, 1));
        gc.setStroke(Color.valueOf("A4802C"));
        gc.strokeOval(chess.getX(), chess.getY(), chess.getW(), chess.getW());
        gc.setLineWidth(1);
        gc.strokeOval(chess.getX(), chess.getY(), chess.getW() - 1, chess.getW() - 1);
        gc.strokeOval(chess.getX() + 5, chess.getY() + 5, chess.getW() - 10, chess.getW() - 10);
        gc.setFill(color);
        gc.fillText(chess.getDisplayName(), chess.getX() + chess.getW() / 2, chess.getY() + chess.getW() / 2 + fontSize / 3);
    }

    //取消选中
    public static void unSelectChess(GraphicsContext gc) {
        //if(selectChess != null ){
        gc.clearRect(0, 0, canvasW, canvasH);
        drawShapes(gc);
        //}
    }

    //选中棋子
    public static void selectChess(Piece chess, GraphicsContext gc) {
        unSelectChess(gc);
        if (!board.getCurrentPlayer().equals(chess.getPlayer())) {
            return;
        }
        selectChess = chess;
        gc.setLineWidth(2);
        gc.setStroke(Color.rgb(1, 1, 1));
        gc.setStroke(Color.valueOf("EAEED9"));
        chess.setSelected(true);
        gc.strokeOval(chess.getX(), chess.getY(), chess.getW(), chess.getW());
    }


    //获取点击的坐标对应的棋子
    public static Piece clickChess(double x, double y) {
        int i = (int) (x / girdW);
        int j = (int) (y / girdH);
        System.out.println(i + " __ " + j);
        return board.getPieceAt(new Position(j, i));
    }

    //画棋盘。画线
    public static void drawPlace(GraphicsContext gc) {
        gc.setLineWidth(3);
        gc.strokeLine(girdW / 3, girdH / 3, girdW / 3, canvasH - girdH / 3);
        gc.strokeLine(girdW / 3 * 2 + girdW * 8, girdH / 3, girdW / 3 * 2 + girdW * 8, canvasH - girdH / 3);
        gc.strokeLine(girdW / 3, girdH / 3 + 0, canvasW - girdW / 3, girdH / 3 + 0);
        gc.strokeLine(girdW / 3, girdH / 3 * 2 + girdH * 9, canvasW - girdW / 3, girdH / 3 * 2 + girdH * 9);
        gc.setLineWidth(1);
        //竖线
        for (int i = 0; i < 9; i++) {
            //河界要断开
            if (i == 0 || i == 8) {
                gc.strokeLine(girdW / 2 + girdW * i, girdH / 2, girdW / 2 + girdW * i, canvasH - girdH / 2);
            } else {
                gc.strokeLine(girdW / 2 + girdW * i, girdH / 2, girdW / 2 + girdW * i, canvasH - girdH / 2 - girdH * 5);
                gc.strokeLine(girdW / 2 + girdW * i, canvasH - girdH / 2 - girdH * 4, girdW / 2 + girdW * i, canvasH - girdH / 2);
            }
        }
        //横线
        for (int i = 0; i < 10; i++) {
            gc.strokeLine(girdW / 2, girdH / 2 + girdH * i, canvasW - girdW / 2, girdH / 2 + girdH * i);
        }


        gc.setTextAlign(TextAlignment.CENTER);
        gc.setFont(new Font(fontName, fontSize));
        gc.fillText("楚河              ", girdW / 2 + girdW * 3, canvasH - girdH * 5 + fontSize / 3);
        //画帅的x
        gc.strokeLine(girdW / 2 + girdW * 3, girdH / 2, girdW / 2 + girdW * 5, girdH / 2 + girdH * 2);
        gc.strokeLine(girdW / 2 + girdW * 3, girdH / 2 + girdH * 2, girdW / 2 + girdW * 5, girdH / 2);
        //画将x
        gc.strokeLine(girdW / 2 + girdW * 3, canvasH - girdH / 2, girdW / 2 + girdW * 5, canvasH - girdH / 2 - girdH * 2);
        gc.strokeLine(girdW / 2 + girdW * 3, canvasH - girdH / 2 - girdH * 2, girdW / 2 + girdW * 5, canvasH - girdH / 2);
        //画炮的标
        gc.strokeLine(girdW / 2 + girdW / 3 * 2, girdH / 2 + girdH * 2 - girdH / 10, girdW / 2 + girdW - girdW / 10, girdH / 2 + girdH * 2 - girdH / 10);
        gc.strokeLine(girdW / 2 + girdW / 3 * 2, girdH / 2 + girdH * 2 + girdH / 10, girdW / 2 + girdW - girdW / 10, girdH / 2 + girdH * 2 + girdH / 10);
        gc.strokeLine(girdW / 2 + girdW + girdW / 10, girdH / 2 + girdH * 2 - girdH / 10, girdW / 2 + girdW + girdW / 3, girdH / 2 + girdH * 2 - girdH / 10);
        gc.strokeLine(girdW / 2 + girdW + girdW / 10, girdH / 2 + girdH * 2 + girdH / 10, girdW / 2 + girdW + girdW / 3, girdH / 2 + girdH * 2 + girdH / 10);

        gc.strokeLine(girdW / 2 + girdW / 3 * 2, girdH / 2 + girdH * 7 - girdH / 10, girdW / 2 + girdW - girdW / 10, girdH / 2 + girdH * 7 - girdH / 10);
        gc.strokeLine(girdW / 2 + girdW / 3 * 2, girdH / 2 + girdH * 7 + girdH / 10, girdW / 2 + girdW - girdW / 10, girdH / 2 + girdH * 7 + girdH / 10);
        gc.strokeLine(girdW / 2 + girdW + girdW / 10, girdH / 2 + girdH * 7 - girdH / 10, girdW / 2 + girdW + girdW / 3, girdH / 2 + girdH * 7 - girdH / 10);
        gc.strokeLine(girdW / 2 + girdW + girdW / 10, girdH / 2 + girdH * 7 + girdH / 10, girdW / 2 + girdW + girdW / 3, girdH / 2 + girdH * 7 + girdH / 10);

        gc.strokeLine(girdW / 2 + girdW / 3 * 2 + girdW * 6, girdH / 2 + girdH * 2 - girdH / 10, girdW / 2 + girdW - girdW / 10 + girdW * 6, girdH / 2 + girdH * 2 - girdH / 10);
        gc.strokeLine(girdW / 2 + girdW / 3 * 2 + girdW * 6, girdH / 2 + girdH * 2 + girdH / 10, girdW / 2 + girdW - girdW / 10 + girdW * 6, girdH / 2 + girdH * 2 + girdH / 10);
        gc.strokeLine(girdW / 2 + girdW + girdW / 10 + girdW * 6, girdH / 2 + girdH * 2 - girdH / 10, girdW / 2 + girdW + girdW / 3 + girdW * 6, girdH / 2 + girdH * 2 - girdH / 10);
        gc.strokeLine(girdW / 2 + girdW + girdW / 10 + girdW * 6, girdH / 2 + girdH * 2 + girdH / 10, girdW / 2 + girdW + girdW / 3 + girdW * 6, girdH / 2 + girdH * 2 + girdH / 10);

        gc.strokeLine(girdW / 2 + girdW / 3 * 2 + girdW * 6, girdH / 2 + girdH * 7 - girdH / 10, girdW / 2 + girdW - girdW / 10 + girdW * 6, girdH / 2 + girdH * 7 - girdH / 10);
        gc.strokeLine(girdW / 2 + girdW / 3 * 2 + girdW * 6, girdH / 2 + girdH * 7 + girdH / 10, girdW / 2 + girdW - girdW / 10 + girdW * 6, girdH / 2 + girdH * 7 + girdH / 10);
        gc.strokeLine(girdW / 2 + girdW + girdW / 10 + girdW * 6, girdH / 2 + girdH * 7 - girdH / 10, girdW / 2 + girdW + girdW / 3 + girdW * 6, girdH / 2 + girdH * 7 - girdH / 10);
        gc.strokeLine(girdW / 2 + girdW + girdW / 10 + girdW * 6, girdH / 2 + girdH * 7 + girdH / 10, girdW / 2 + girdW + girdW / 3 + girdW * 6, girdH / 2 + girdH * 7 + girdH / 10);

        gc.strokeLine(girdW / 2 + girdW - girdW / 10, girdH / 2 + girdH + girdH / 3 * 2, girdW / 2 + girdW - girdW / 10, girdH / 2 + girdH * 2 - girdH / 10);
        gc.strokeLine(girdW / 2 + girdW - girdW / 10, girdH / 2 + girdH * 2 + girdH / 10, girdW / 2 + girdW - girdW / 10, girdH / 2 + girdH * 2 + girdH / 3);
        gc.strokeLine(girdW / 2 + girdW + girdW / 10, girdH / 2 + girdH + girdH / 3 * 2, girdW / 2 + girdW + girdW / 10, girdH / 2 + girdH * 2 - girdH / 10);
        gc.strokeLine(girdW / 2 + girdW + girdW / 10, girdH / 2 + girdH * 2 + girdH / 10, girdW / 2 + girdW + girdW / 10, girdH / 2 + girdH * 2 + girdH / 3);

        gc.strokeLine(girdW / 2 + girdW - girdW / 10, girdH / 2 + girdH * 6 + girdH / 3 * 2, girdW / 2 + girdW - girdW / 10, girdH / 2 + girdH * 7 - girdH / 10);
        gc.strokeLine(girdW / 2 + girdW - girdW / 10, girdH / 2 + girdH * 7 + girdH / 10, girdW / 2 + girdW - girdW / 10, girdH / 2 + girdH * 7 + girdH / 3);
        gc.strokeLine(girdW / 2 + girdW + girdW / 10, girdH / 2 + girdH * 6 + girdH / 3 * 2, girdW / 2 + girdW + girdW / 10, girdH / 2 + girdH * 7 - girdH / 10);
        gc.strokeLine(girdW / 2 + girdW + girdW / 10, girdH / 2 + girdH * 7 + girdH / 10, girdW / 2 + girdW + girdW / 10, girdH / 2 + girdH * 7 + girdH / 3);


        gc.strokeLine(girdW / 2 + girdW * 7 - girdW / 10, girdH / 2 + girdH + girdH / 3 * 2, girdW / 2 + girdW * 7 - girdW / 10, girdH / 2 + girdH * 2 - girdH / 10);
        gc.strokeLine(girdW / 2 + girdW * 7 - girdW / 10, girdH / 2 + girdH * 2 + girdH / 10, girdW / 2 + girdW * 7 - girdW / 10, girdH / 2 + girdH * 2 + girdH / 3);
        gc.strokeLine(girdW / 2 + girdW * 7 + girdW / 10, girdH / 2 + girdH + girdH / 3 * 2, girdW / 2 + girdW * 7 + girdW / 10, girdH / 2 + girdH * 2 - girdH / 10);
        gc.strokeLine(girdW / 2 + girdW * 7 + girdW / 10, girdH / 2 + girdH * 2 + girdH / 10, girdW / 2 + girdW * 7 + girdW / 10, girdH / 2 + girdH * 2 + girdH / 3);

        gc.strokeLine(girdW / 2 + girdW * 7 - girdW / 10, girdH / 2 + girdH * 6 + girdH / 3 * 2, girdW / 2 + girdW * 7 - girdW / 10, girdH / 2 + girdH * 7 - girdH / 10);
        gc.strokeLine(girdW / 2 + girdW * 7 - girdW / 10, girdH / 2 + girdH * 7 + girdH / 10, girdW / 2 + girdW * 7 - girdW / 10, girdH / 2 + girdH * 7 + girdH / 3);
        gc.strokeLine(girdW / 2 + girdW * 7 + girdW / 10, girdH / 2 + girdH * 6 + girdH / 3 * 2, girdW / 2 + girdW * 7 + girdW / 10, girdH / 2 + girdH * 7 - girdH / 10);
        gc.strokeLine(girdW / 2 + girdW * 7 + girdW / 10, girdH / 2 + girdH * 7 + girdH / 10, girdW / 2 + girdW * 7 + girdW / 10, girdH / 2 + girdH * 7 + girdH / 3);


    }

    public static void drawShapes(GraphicsContext gc) {
        gc.setFill(Color.GREEN);
        gc.setStroke(Color.BLACK);
        gc.setLineWidth(1);
        gc.setTextAlign(TextAlignment.CENTER);
        gc.setFont(new Font("", fontSize));
        drawPlace(gc);
        drawChess(gc);
    }
}